from dao.ticket import TicketDao
from lib.validator import Validator
from lib.form import Form

from flask import request
from flask_login import login_user, current_user
from bson.dbref import DBRef

class TicketService():
    def __init__(self):
        """Arguments:
            name -- string type
            item_id -- BSON ObjectId type
        """
        self.dao = TicketDao()
        
    def save_tickets(self, form):
        """ Generate ticket serials number and save to database.
        Return:
            An array of tickets.
        """
        tickets = []
        
        if form.errors == []:
            user_ref = DBRef('users', current_user._id)
            for i in range(form.n_adults):
                _id = self.dao.save({'type':'Adult',
                             'status':'booked',
                             'user':user_ref})
                if _id is not None:
                    tickets.append({'id':str(_id), 'type':'Adult', 'status':'booked', 'user':current_user.username, 'email':current_user.email})
                
            for i in range(form.n_seniors):
                _id = self.dao.save({'type':'Senior',
                             'status':'booked',
                             'user':user_ref})
                if _id is not None:
                    tickets.append({'id':str(_id), 'type':'Senior', 'status':'booked', 'user':current_user.username, 'email':current_user.email})
                
            for i in range(form.n_children):
                _id = self.dao.save({'type':'Child',
                             'status':'booked',
                             'user':user_ref})
                if _id is not None:
                    tickets.append({'id':str(_id), 'type':'Child', 'status':'booked', 'user':current_user.username, 'email':current_user.email})
        return tickets
            
class TicketValidator(Validator):
    def __init__(self):
        Validator.__init__(self)
              
        self.rules = {'n-adult-tickets': {
                                'numeric':[None, True],
                                'min':[None, 0],
                                'max':[None, 30]
                                },
                      'n-senior-tickets':{
                                'numeric':[None, True],
                                'min':[None, 0],
                                'max':[None, 30]
                               },
                      'n-child-tickets': {
                                'numeric':[None, True],
                                'min':[None, 0],
                                'max':[None, 30]
                                }
        }
            
class TicketForm(Form):
    """ Submit user form
    """
    def __init__(self):
        """Only accept POST request
        """
        Form.__init__(self)
        self.validator = TicketValidator()
        
        if self.is_submitted():
            inputs = self.get_raw_inputs()
            self.errors = self.validator.validate(inputs)
            if self.errors == []:
                self.n_adults = int(self.request.values.get('n-adult-tickets'))
                self.n_seniors = int(self.request.values.get('n-senior-tickets'))
                self.n_children = int(self.request.values.get('n-child-tickets'))
                
    def has_error(self):
        return self.errors == []            
            
